package com.yyfax.pay.config.data.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.json.JacksonUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.common.constants.CacheName;
import com.yyfax.pay.config.data.dao.BankConfigDao;
import com.yyfax.pay.config.data.dao.BankConfigExtDao;
import com.yyfax.pay.config.data.service.BankConfigExtService;
import com.yyfax.pay.config.data.service.BankConfigService;
import com.yyfax.pay.config.model.po.BankConfigExtPO;
import com.yyfax.pay.config.model.po.BankConfigPO;

@Service
public class BankConfigServiceImpl extends BaseServiceImpl implements BankConfigService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BankConfigDao bankConfigDao;
	
	@Autowired
	private BankConfigExtService bankConfigExtService;
	

	public BankConfigServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	@Override
	public int save(BankConfigPO config) throws Exception {
		if (config == null) {
			logger.error("新增银行信息传入数据为空返回自增主键 -1");
			return -1;
		}
		clearCache(CacheName.BANK_CONFIG);
		return bankConfigDao.insert(config);
	}

	@Override
	public int modify(BankConfigPO config) throws Exception {
		if (config == null) {
			logger.error("修改银行信息传入数据为空返回  -1");
			return -1;
		}
		clearCache(CacheName.BANK_CONFIG);
		return bankConfigDao.update(config);
	}

	@SuppressWarnings("unchecked")
	@Override
	public BankConfigPO queryByBankCode(String bankCode) throws Exception {
		String json = getCache(CacheName.BANK_CONFIG);
		if (StringUtils.isNotBlank(json)) {
			List<BankConfigPO> list = (List<BankConfigPO>) JacksonUtil.fromJson(json, List.class, BankConfigPO.class);
			for (BankConfigPO po : list) {
				if (bankCode.equals(po.getBankCode())) {
					return po;
				}
			}

		}else{
			queryForList();
		}
		logger.info("内存数据为空从数据库获取银行信息");
		return bankConfigDao.selectByBankCode(bankCode);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankConfigPO> queryForList() throws Exception {
		// 从缓存读取数据
		List<BankConfigPO> pos = null;
		String json = getCache(CacheName.BANK_CONFIG);
		if (StringUtils.isNotBlank(json)) {
			pos = (List<BankConfigPO>) JacksonUtil.fromJson(json, List.class, BankConfigPO.class);
			return pos;
		}
		logger.info("缓存数据为空从数据库获取银行信息");
		pos = bankConfigDao.selectForList();
		if (pos != null && !pos.isEmpty()) {
			cacheService.set(CacheName.BANK_CONFIG, JacksonUtil.toJson(pos));
		}
		return pos;
	}

	@Override
	public BankConfigPO queryById(int id) throws Exception {
		if (id <= 0) {
			logger.error("参数id为{},小于或等于0", id);
			return null;
		}
		return bankConfigDao.selectById(id);
	}

	@Override
	public BankConfigPO queryByThirdBankCode(String paymentCode, String thirdBankCode) throws Exception {
		if (StringUtils.isBlank(paymentCode) || StringUtils.isBlank(thirdBankCode)) {
			logger.error("根据paymentCode，thirdBankCode获取银行信息，有参数为空，传入paymentCode为：{}，thirdBankCode为：{}", paymentCode, thirdBankCode);
			return null;
		}
		
		BankConfigExtPO bankConfigExtPO = bankConfigExtService.queryByPaymentAndBankCodeExt(paymentCode,thirdBankCode);
		if (bankConfigExtPO == null) {
			return null;
		}
		
		String json = getCache(CacheName.BANK_CONFIG);
		if (StringUtils.isNotBlank(json)) {
			List<BankConfigPO> list = (List<BankConfigPO>) JacksonUtil.fromJson(json, List.class, BankConfigPO.class);
			for (BankConfigPO po : list) {
				if (bankConfigExtPO.getBankCode().equals(po.getBankCode())) {
					return po;
				}
			}
		}else{
			queryForList();
		}
		
		return bankConfigDao.selectByBankCode(bankConfigExtPO.getBankCode());
	}

	// @Override
	// public BankConfigVO queryVOByPaymentAndBankCodeExt(String paymentCode, String bankCodeExt) throws Exception {
	// if (StringUtils.isBlank(paymentCode) || StringUtils.isBlank(bankCodeExt)) {
	// logger.debug("根据paymentCode，thirdBankCode获取银行信息，有参数为空，传入paymentCode为：{}，bankCodeExt为：{}", paymentCode, bankCodeExt);
	// return null;
	// }
	//
	// BankConfigExtPO bankConfigExtPO = bankConfigExtDao.selectByPaymentAndBankCodeExt(paymentCode, bankCodeExt);
	// if (bankConfigExtPO == null) {
	// return null;
	// }
	//
	// BankConfigPO bankConfigPO = bankConfigDao.selectByBankCode(bankConfigExtPO.getBankCode());
	// if (bankConfigPO == null) {
	// return null;
	// }
	//
	// BankConfigVO vo = new BankConfigVO();
	// BeanUtil.copyProperties(bankConfigPO, vo);
	//
	// List<BankConfigExtPO> bankConfigExtPOs = bankConfigExtDao.selectListByBankCodeAndPayment(bankConfigExtPO.getBankCode(), paymentCode);
	// vo.setBankConfigExtPOs(bankConfigExtPOs);
	//
	// return vo;
	// }
}
